import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class ListViewDemoIII extends StatefulWidget {
  const ListViewDemoIII({Key? key}) : super(key: key);

  @override
  State<ListViewDemoIII> createState() => _ListViewDemoIIIState();
}

class _ListViewDemoIIIState extends State<ListViewDemoIII> {

  List list = [];
  RefreshController refreshController = new RefreshController();

  Widget defaultHeader = defaultTargetPlatform == TargetPlatform.iOS
  ?ClassicHeader(
    idleText: "下拉刷新",
    refreshingText: "刷新中",
    releaseText: "释放刷新",
    failedText: "刷新失败",
    completeText: "刷新成功",
  )
  :MaterialClassicHeader();

  final LoadIndicator defaultFooter = ClassicFooter(
    failedText: "加载失败",
    idleText: "上拉加载更多",
    canLoadingText: "取消加载",
    loadingText: "加载中",
    noDataText: "暂无更多数据",
  );
 
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    for(int i = 0;i<50;i++){
      list.add("items $i");
    }
   

  }
  @override
  Widget build(BuildContext context) {
    
    return Scaffold(
      appBar: AppBar(
        title: Text("下拉刷新组件SmartRefresher使用分析"),
      ),
      //  刷新组件
      body:SmartRefresher(

        header: defaultHeader,
        footer: defaultFooter,
        // 是否可以上拉加载
        enablePullUp: true,
        // 是否可以下拉刷新
        enablePullDown: true,
        // 控制器
        controller:refreshController,
        child: buildListView(),
        onRefresh: (){
          print("下拉刷新回调");
          Future.delayed(new Duration(microseconds: 3000),(){
            refreshController.refreshCompleted();
          });
        },
        onLoading: (){
          print("上拉加载回调");
          Future.delayed(new Duration(microseconds: 3000),(){
            refreshController.loadComplete();
          });
        },
        )

    );
    
  }
  
  //  加载 列表式布局 自带分隔线
  Widget buildListView(){
    return ListView.builder(
        itemBuilder: (BuildContext context,int index){
          // 用来构建 子条目
          // index 是条目的下标
          return Container(
            padding: EdgeInsets.all(10),
            color: Colors.grey[300],
            height: 88,
            margin: EdgeInsets.all(5),
            child: Text(list[index]),
          );
        },
        // 数据的个数
        itemCount: list.length,

        );
  }
  
}